#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <assert.h>

using namespace std;

enum Color
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTNode
{
	RBTNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _col(RED)
	{}

	RBTNode<K, V>* _left;
	RBTNode<K, V>* _right;
	RBTNode<K, V>* _parent;
	pair<K, V> _kv;
	Color _col;
};

template<class K, class V>
class RBTree
{
public:
	typedef RBTNode<K, V> Node;

	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else return false;
		}

		Node* newnode = new Node(kv);
		newnode->_parent = parent;
		if (kv.first < parent->_kv.first) parent->_left = newnode;
		else parent->_right = newnode;

		cur = newnode;
		
		while (cur->_parent && cur->_parent->_col == RED)
		{
			Node* father = cur->_parent;
			Node* grandparent = father->_parent;
			if (father == grandparent->_left)
			{
				Node* uncle = grandparent->_right;
				if (uncle && uncle->_col == RED)
				{
					father->_col = uncle->_col = BLACK;
					grandparent->_col = RED;

					cur = grandparent;
				}
				else
				{
					if (cur == father->_left)
					{
						RotateR(grandparent);
						father->_col = BLACK;
						grandparent->_col = RED;
					}
					else
					{
						RotateL(father);
						RotateR(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandparent->_left;
				if (uncle && uncle->_col == RED)
				{
					father->_col = uncle->_col = BLACK;
					grandparent->_col = RED;

					cur = grandparent;
				}
				else
				{
					if (cur == father->_right)
					{
						RotateL(grandparent);
						father->_col = BLACK;
						grandparent->_col = RED;
					}
					else
					{
						RotateR(father);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;

		return true;
	}

	void RotateR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* grandparent = parent->_parent;
		Node* curR = cur->_right;

		parent->_left = curR;
		if (curR) curR->_parent = parent;
		parent->_parent = cur;
		cur->_right = parent;

		cur->_parent = grandparent;
		if (grandparent)
		{
			if (parent == grandparent->_left) grandparent->_left = cur;
			else grandparent->_right = cur;
		}
		else _root = cur;
	}

	void RotateL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* grandparent = parent->_parent;
		Node* curL = cur->_left;

		parent->_right = curL;
		if (curL) curL->_parent = parent;
		parent->_parent = cur;
		cur->_left = parent;

		cur->_parent = grandparent;
		if (grandparent)
		{
			if (parent == grandparent->_left) grandparent->_left = cur;
			else grandparent->_right = cur;
		}
		else _root = cur;
	}

	Node* find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_key) cur = cur->_left;
			else if (key > cur->_key) cur = cur->_right;
			else return cur;
		}
		return nullptr;
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	bool IsBalance()
	{
		if (_root->_col == RED) return false;

		int leftNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK) leftNum++;
			cur = cur->_left;
		}

		int blackNum = 0;
		return _IsBalance(_root, blackNum, leftNum);
	}

protected:
	bool _IsBalance(Node* root, int blackNum, int& leftNum)
	{
		if (root == nullptr)
		{
			if (blackNum != leftNum)
			{
				cout << "the number of black node is not same" << endl;
				return false;
			}
			return true;
		}

		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "there are two continuous node that their color is red" << endl;
			return false;
		}

		if (root->_col == BLACK) blackNum++;

		return _IsBalance(root->_left, blackNum, leftNum)
			&& _IsBalance(root->_right,blackNum, leftNum);
	}

	void _InOrder(const Node* root)
	{
		if (root == nullptr) return;
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	Node* _root = nullptr;
};

void Test_RBTree1()
{
	int a[] = { 5,3,8,4,9,1,2,7,6 };
	RBTree<int, int> tree;
	for (auto& e : a)
		tree.insert({ e,e });

	tree.InOrder();
}

void Test_RBTree2() 
{
	srand(time(nullptr));
	int N = 1000000;
	RBTree<int, int> tree;

	for (int i = 0; i < N; i++)
	{
		int num = rand() + i;
		tree.insert({ num, num });
	}

	cout << tree.IsBalance() << endl;
}